package leetcode.editor.cn;

import java.util.*;
import leetcode.editor.common.*;

public class MinStack {

    //leetcode submit region begin(Prohibit modification and deletion)
    class MinStack2 {
          // 使用 2个栈，一个用来 真实存储元素，另一个用来 存储最小值 （当前元素入栈后，栈中的最小值）
       Stack<Integer>  holdStack ;
       Stack<Integer>  minStack ;

        public MinStack2() {
           this.holdStack = new Stack<>();
           this.minStack = new Stack<>();
        }
        
        public void push(int val) {
            this.holdStack.push(val);
            if(minStack.isEmpty()){
                this.minStack.push(val);
            }else {
                if(val<=minStack.peek()){
                    this.minStack.push(val);
                }
            }
        }
        
        public void pop() {

            int val = this.holdStack.peek();
            if(!this.holdStack.isEmpty()){
                // 弹出元素
                this.holdStack.pop();
            }

            // 容器栈中的元素 被删除，需要校验 是否 为最小元素栈中的值
            if(val==minStack.peek()){
                this.minStack.pop();
            }



        }
        
        public int top() {
            if(!this.holdStack.isEmpty()){
                // 弹出元素
             return    this.holdStack.peek();
            }
            return -1;
        }
        
        public int getMin() {
         return    this.minStack.isEmpty()?-1:this.minStack.peek();
        }
    }
    
    /**
     * Your MinStack object will be instantiated and called as such:
     * MinStack obj = new MinStack();
     * obj.push(val);
     * obj.pop();
     * int param_3 = obj.top();
     * int param_4 = obj.getMin();
     */
    //leetcode submit region end(Prohibit modification and deletion)

    
    public static void main(String[] args) {
        // put your test code here
    }
}
